
import os
from time import time

class sync_member_node:
    def  __init__(self, parent, name, last_m_time):
        self.parent = parent
        self.folders = []
        self.files = []
        self.node_name = name
        self.last_update_time_local = last_m_time
        self.last_update_time_remote = last_m_time
        self.is_deleted = False

    
    def get_path(self):
        path_to_root = [self.node_name]
        current = self.parent
        while current != None:
            path_to_root.append(current.node_name)
            current = current.parent
        return "\\".join(path_to_root)

    def to_list(self):
        ret = [self.node_name]
        self.all_children(self.node_name + os.sep, ret)
        return ret

    def __str__(self):
        ret = [self.node_name]
        self.all_children(self.node_name + os.sep, ret)
        return "\r\n".join(ret)

    def all_children(self, prefix, ret):
        for i in self.folders:
            ret.append(prefix + i.node_name)
            i.all_children(prefix + i.node_name + os.sep , ret)
        for j in self.files:
            ret.append(prefix + j.node_name)
    def __len__(self):
        ret = 1 + len(self.files)
        for i in self.folders:
            ret = ret + len(i)
        return ret

def scan(root):
    folders_cache = {}
    root_obj = sync_member_node(None, root, os.path.getmtime(root))
    folders_cache[root] = root_obj
    for dirpath, dirnames, filenames in os.walk(root):
        parent_folder = folders_cache[dirpath]
        for folder_name in dirnames:
            child_folder = sync_member_node(parent_folder, folder_name, os.path.getmtime(os.path.join(dirpath, folder_name)))
            parent_folder.folders.append(child_folder)
            folders_cache[os.path.join(dirpath, folder_name)] = child_folder
        for file_name in filenames:
            child_file = sync_member_node(parent_folder, file_name, os.path.getmtime(os.path.join(dirpath, file_name)))
            parent_folder.files.append(child_file)
    return root_obj


start = time()
allf = scan("E:\\")
print(len(allf))
print(("scan time : ", time() - start))
start = time()


import sqlite3
conn = sqlite3.connect('example.db')


c = conn.cursor()



#c.execute('''CREATE TABLE IF NOT EXISTS sync_history( local_path TEXT, item_size INTEGER, local_last_update_time INTEGER, cloud_last_update_time INTEGER, is_deleted INTEGER)''')
#c.execute('''DELETE FROM sync_history''')
#print(("delete time : ", time() - start))


start = time()
MAX_INT = 2**31
insert_statment = "INSERT INTO sync_history values(?,?,?,?,?)"
insert_values = []
for file_path in allf.to_list():
    statbuf = os.stat(file_path)
    insert_values.append((file_path, statbuf.st_size, int(statbuf.st_mtime * 1000 % MAX_INT ), int(statbuf.st_mtime * 1000 % MAX_INT), 0))

c.executemany(insert_statment, insert_values)
print(("insert time : ", time() - start))
start = time()
row_count = 0
for row in c.execute('SELECT * FROM sync_history'):
        row_count += 1

print row_count

print(("select time : ", time() - start))
start = time()

conn.commit()
c.close()